package com.ywgt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywgt.pojo.Coupon;
import com.ywgt.mapper.CouponMapper;
import com.ywgt.pojo.CouponUser;
import com.ywgt.service.CouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywgt.service.CouponUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 优惠券信息及规则表 服务实现类
 * </p>
 *
 * @author yiping
 * @since 2021-03-13
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements CouponService {

    @Autowired
    CouponUserService couponUserService;
    @Override
    public Page<Coupon> getCoupons(String name, Integer type, Integer status, int page, int limit, String sort, String order) {
        PageHelper.startPage(page,limit);
        return baseMapper.selectCoupons(name,type,status,sort,order);
    }
    @Override
    public List<Coupon> queryAvailableList(Integer userId, int offset, int limit){
        Map<String,Object> searchMap=new HashMap<>();
        searchMap.put("user_id",userId);
        List<CouponUser> couponUsers = couponUserService.listByMap(searchMap);
        LambdaQueryWrapper<Coupon> wrapper=new LambdaQueryWrapper<>();
        List<Integer> ids = couponUsers.stream().map(CouponUser::getCouponId).distinct().collect(Collectors.toList());
        PageHelper.startPage(offset,limit);
        Page<Coupon>coupons=baseMapper.selectAvailableCoupons(ids,offset,limit);
        return coupons.getResult();
    }
}
